package com.blank.shop.hystrix.provider;

import com.blank.shop.hystrix.ShopHystrixApplication;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.temporal.ChronoUnit;
import java.util.concurrent.TimeUnit;

/**
 * 服务提供者
 * <br/>Date 2021/8/31
 * <br/>Time 14:03:04
 *
 * @author _blank
 */
@Slf4j
@Profile({ShopHystrixApplication.SERVER_PROVIDER})
@RestController
public class HystrixProviderController {

    @Value("${server.port}")
    private Integer serverPort;

    /**
     * 模拟正常请求
     *
     * @return org.springframework.http.ResponseEntity
     */
    @PostMapping(path = {"simulation-normal"})
    public ResponseEntity<String> simulationNormal() {
        final String threadName = Thread.currentThread().getName();
        return ResponseEntity.ok("当前端口：" + this.serverPort + "，当前线程：" + threadName + "请求正常！");
    }

    /**
     * 模拟超时请求
     *
     * @return org.springframework.http.ResponseEntity
     */
    @HystrixCommand(
            // 失败兜底调用的方法名称
            fallbackMethod = "simulationTimeOutFallbackHandler",
            commandProperties = {
                    // 服务降级
                    @HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS, value = "3000")
                    // 服务熔断 所有的配置参考这里 @see com.netflix.hystrix.HystrixCommandProperties
                    , @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_ENABLED, value = "true")// 是否开启断路器
                    , @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_REQUEST_VOLUME_THRESHOLD, value = "10")// 请求次数
                    , @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_SLEEP_WINDOW_IN_MILLISECONDS, value = "10000")// 时间窗口期
                    , @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_ERROR_THRESHOLD_PERCENTAGE, value = "60")// 失败率达到多少后跳闸
            }
    )
    @PostMapping(path = {"simulation-time-out"})
    public ResponseEntity<String> simulationTimeOut() {
        final long timeout = 5L;
        try {
            TimeUnit.SECONDS.sleep(timeout);
        } catch (InterruptedException e) {
            log.error(e.getMessage(), e);
        }
        final String threadName = Thread.currentThread().getName();
        return ResponseEntity.ok("当前端口：" + this.serverPort + "，当前线程：" + threadName + "请求缓慢，耗时：" + timeout + " " + ChronoUnit.SECONDS + "！");
    }

    public ResponseEntity<String> simulationTimeOutFallbackHandler() {
        final String threadName = Thread.currentThread().getName();
        return ResponseEntity.ok("当前端口：" + this.serverPort + "，当前线程：" + threadName + "，simulationTimeOut处理失败，执行兜底方法！");
    }


}
